---@class InvasionStatisticsItem : UIComponentBase
local InvasionStatisticsItem = Class("InvasionStatisticsItem",UIComponentBase)
local this = InvasionStatisticsItem
function InvasionStatisticsItem:InitComponent(playerGoList)
    ---@type InvasionStatisticsItem_Player[]
    self.playerItemList = {}
    for k,v in pairs(playerGoList) do
        local item = this.PlayerItem.New(v,k)
        table.insert(self.playerItemList,item)
    end
    self.players = {}
end
function InvasionStatisticsItem:InitPlayer()
    local players = TBS.ActorMgr.GetLeaderList()
    table.clear(self.players)
    if players then
        for k,v in pairs(players) do
            table.insert(self.players,{id = v:GetId(),damage = 0})
        end
    end
    self:Refresh()
end
function InvasionStatisticsItem.Sort(a,b)
    return a.damage > b.damage
end
function InvasionStatisticsItem:Statistics(actorId,damage)
    if damage <= 0 then return end
    local player = TBS.ActorMgr.GetActor(actorId)
    if player:IsBoss() then return end

    local leader
    if player:IsLeader() then
        leader = player
    else
        leader = TBS.ActorMgr.GetGroupLeader(player:GetGroupId())
    end
    for k,v in pairs(self.players) do
        if v.id == leader:GetId() then
            v.damage = v.damage + damage
        end
    end
    --table.sort(self.players,InvasionStatisticsItem.Sort)
    self:Refresh()
end
function InvasionStatisticsItem:Refresh()
    for k,v in pairs(self.playerItemList) do
        local p = self.players[k]
        if p then
            v:SetDamage(p.id,p.damage)
        else
            v:Empty()
        end
    end
end
function InvasionStatisticsItem:OnDestroy()
    if self.playerItemList then
        for k,v in pairs(self.playerItemList) do
            v:Destroy()
        end
    end
    self.players = nil
end
---@class InvasionStatisticsItem_Player:UIComponentBase
local PlayerItem = Class("PlayerItem",UIComponentBase)
InvasionStatisticsItem.PlayerItem = PlayerItem
function PlayerItem:OnCreate(index)
    self.ctrlRank = self:GetController("rank")
    self.ctrlState = self:GetController("state")
    self.ctrlIfMe = self:GetController("ifMe")
    self.nameText = self:GetChild("name")
    self.tipText = self:GetChild("tips")
    self.numText = self:GetChild("num")
    self.ctrlRank.selectedIndex=tonumber(index-1)
    self.index = index
    self.defaultColor = self.nameText.color
end
function PlayerItem:SetDamage(actorId,damage)
    local player = TBS.ActorMgr.GetActor(actorId)
    self.ctrlState.selectedIndex = 1
    if logicMgr.HeroManager.CompareMineId(player.playerId) then
        self.ctrlIfMe.selectedIndex = 1
    else
        self.ctrlIfMe.selectedIndex = 0
    end
    self.nameText.text = player.name
    logicMgr.ChatManager.GMColor(self.nameText,player.name,self.defaultColor)
    self.numText.text = damage
end
function PlayerItem:Empty()
    self.ctrlState.selectedIndex = 0
end

return InvasionStatisticsItem